<template>
	<view class="package">
		<view class="top">
			<view class="charts">
				<canvas style="width: 100%; height: 100%;" type="2d" id="homeownerCanvas"
					class="homeowner-canvas_charts"></canvas>
			</view>
			<!-- 进度条 -->
			<view class="progress">
				<view class="progressItem" v-for="(item,index) in progressData" :key="index">
					<text class="text">{{item.name}}</text>
					<progress :percent="item.percent" stroke-width="3" :activeColor="item.color" />
					<text class="text">{{item.currentProgress}} / {{item.overallProgress}}</text>
				</view>
			</view>
		</view>
		<view class="center">
			<ppc-section style="width: 100%;" :title="'套餐内容'" :value="caloric+'大卡'"></ppc-section>
		</view>
		<view class="bottom">
			<view class="btn" @click="addFood">
				<image class="image" src="../../static/icon/add.png" mode="aspectFit"></image>
				<text class="text">添加食物</text>
			</view>
			<view class="foodList">
				<ppc-food-card @click="foodClick" :dataSource="data"></ppc-food-card>
			</view>
		</view>
		<view class="saveBg">
			<view class="saveBtn">
				<text class="text">完成添加</text>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		onMounted,
		reactive,
		ref,
		getCurrentInstance,
		nextTick
	} from 'vue';
	import canvas from '../../tools/canvas.js'
	import {
		onReady,
		onLoad
	} from '@dcloudio/uni-app'
	import ppcSection from '/components/ppcSection.vue'
	import ppcFoodCard from '/components/ppcFoodCard.vue'
	const canvasData = ref([{
			"value": 0.333333,
			"color": "#FC8452",
			"title": "碳水"
		},
		{
			"value": 0.333333,
			"color": "#91CB74",
			"title": "蛋白质"
		},
		{
			"value": 0.333333,
			"color": "#FAC858",
			"title": "脂肪"
		}
	])
	// 食物信息
	const data = ref([])
	//总热量
	const caloric = ref(0)
	//进度条信息
	const progressData = reactive([{
		name: "总热量(千卡)",
		percent: 0,
		color: "#1890FF",
		currentProgress: 0,
		overallProgress: uni.getStorageSync("userInfo").metabolize
	}, {
		name: "碳水(g)",
		percent: 0,
		color: "#FC8452",
		currentProgress: 0,
		overallProgress: uni.getStorageSync("userInfo").carbohydrate
	}, {
		name: "蛋白质(g)",
		percent: 0,
		color: "#91CB74",
		currentProgress: 0,
		overallProgress: uni.getStorageSync("userInfo").protein
	}, {
		name: "脂肪(g)",
		percent: 0,
		color: "#FAC858",
		currentProgress: 0,
		overallProgress: uni.getStorageSync("userInfo").fat
	}])
	onReady(() => {
		const instance = getCurrentInstance()
		// canvas.canvasGraph('homeownerCanvas', canvasData.value, caloric.value, instance, 45, 60)
		let query = uni.createSelectorQuery().in(instance).select(`#homeownerCanvas`).fields({
			node: true,
			size: true
		}).exec(res => {
			const width = res[0].width
			const height = res[0].height
			canvas.canvasGraph('homeownerCanvas', canvasData.value, caloric.value, instance, (height -
					20) / 3,
				(height / 3))
		})
	})
	onLoad((option) => {
		if (option.data) {
			const oldData = uni.getStorageSync('package')
			const newData = [...JSON.parse(option.data), ...oldData]
			uni.setStorageSync('package', newData)
			let carbohydrate_
			let protein_
			let fat_
			data.value = [...uni.getStorageSync('package'), ...data.value]
			console.log(data.value);
			data.value.forEach(item => {
				caloric.value += Number(item.totalCaloric)
				progressData.forEach(items => {
					if (items.name == '总热量(千卡)') {
						items.currentProgress = Number(Number(items.currentProgress) + Number(item
								.caloric))
							.toFixed(1)
						items.percent = ((items.currentProgress / items.overallProgress) * 100)
							.toFixed(0)
					}
					if (items.name == '碳水(g)') {
						items.currentProgress = Number(Number(items.currentProgress) + item
								.carbohydrate)
							.toFixed(1)
						items.percent = ((items.currentProgress / items.overallProgress) * 100)
							.toFixed(0)
						carbohydrate_ = Number(Number(items.currentProgress) + item
								.carbohydrate)
							.toFixed(1)

					}
					if (items.name == '蛋白质(g)') {
						items.currentProgress = Number(Number(items.currentProgress) + item
								.protein)
							.toFixed(1)
						items.percent = ((items.currentProgress / items.overallProgress) * 100)
							.toFixed(0)
						protein_ = Number(Number(items.currentProgress) + item
								.protein)
							.toFixed(1)
					}
					if (items.name == '脂肪(g)') {
						items.currentProgress = Number(Number(items.currentProgress) + item.fat)
							.toFixed(1)
						items.percent = ((items.currentProgress / items.overallProgress) * 100)
							.toFixed(0)
						fat_ = Number(Number(items.currentProgress) + item.fat)
							.toFixed(1)
					}

				})
			})
			const sum = Number(carbohydrate_) + Number(protein_) + Number(fat_)
			canvasData.value.forEach(item => {
				if (item.title == '碳水') {
					item.value = (carbohydrate_ / sum)
				}
				if (item.title == '蛋白质') {
					item.value = (protein_ / sum)
				}
				if (item.title == '脂肪') {
					item.value = (fat_ / sum)
				}
			})
			const instance = getCurrentInstance()
			let query = uni.createSelectorQuery().in(instance).select(`#homeownerCanvas`).fields({
				node: true,
				size: true
			}).exec(res => {
				const width = res[0].width
				const height = res[0].height
				canvas.canvasGraph('homeownerCanvas', canvasData.value, caloric.value, instance, (height -
						10) / 3,
					(height / 3))
			})
			// canvas.canvasGraph('homeownerCanvas', canvasData.value, caloric.value, instance, 45, 60)
		} else {
			uni.removeStorageSync('package')
		}
	})
	onMounted(() => {

	})
	//添加食物
	const addFood = () => {
		uni.redirectTo({
			url: '/pages/foods/foods?type=套餐'
		})
	}
</script>

<style lang="scss">
	.package {
		height: 100vh;
		overflow-y: auto;
	}

	.top {
		height: 700rpx;
		background: #FFF;
		margin: 0 30rpx;
		border-radius: 20rpx;

		.charts {
			height: 60%;
		}

		.progress {
			height: 30%;
			display: flex;
			flex-wrap: wrap;
			margin: 5% 20rpx;

			.progressItem {

				width: 45%;
				margin: 0 16rpx;

				.text {
					font-size: 28rpx;
					color: #afafaf;
				}
			}
		}
	}

	.center {
		display: flex;
		height: 10%;
		align-items: center;
		justify-content: flex-start;
	}

	.bottom {
		height: 30%;
		overflow: auto;
		display: flex;
		flex-direction: column;

		.foodList {
			margin: 0 20rpx;
		}

		.btn {
			height: 70rpx;
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
			line-height: 70rpx;
			border-top: 1rpx solid #ebebeb;
			border-bottom: 1rpx solid #ebebeb;

			.image {
				width: 40rpx;
				height: 40rpx;
				margin: 0 10rpx;
			}

			.text {
				font-size: 30rpx;
				font-weight: bold;
				color: #87C38F;
			}
		}
	}

	.saveBg {
		width: 100%;
		height: 10%;
		position: fixed;
		left: 50%;
		bottom: -5%;
		display: flex;
		align-items: flex-start;
		justify-content: center;
		transform: translate(-50%, -50%);
		box-shadow: 0 0 30rpx 0 #ddd;

		.saveBtn {
			width: 90%;
			height: 60%;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-top: 2%;
			background-color: #87C38F;
			border-radius: 45rpx;

			.text {
				font-size: 32rpx;
				color: #ebebeb;
				font-weight: bold;
				letter-spacing: 3rpx;
			}
		}
	}
</style>